<!--
  Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.

  https://www.mall4j.com/

  未经允许，不可做商业用途！

  版权所有，侵权必究！
-->

<template>
  <view class="Mall4j prod-application">
    <uni-forms>
      <uni-forms-item
        name="creditCode"
        class="setting-info-item"
      >
        <view class="title">
          <text class="txt">
            处理人
          </text>
        </view>
        <view class="input-box">
          {{ dataForm.handler }}
        </view>
      </uni-forms-item>
      <uni-forms-item
        name="firmName"
        class="setting-info-item"
      >
        <view class="title">
          <text class="txt">
            处理状态
          </text>
        </view>
        <view class="input-box">
          {{ statusList[dataForm.status] }}
        </view>
      </uni-forms-item>
      <uni-forms-item
        name="residence"
        class="setting-info-item"
      >
        <view class="title">
          <text class="txt">
            下线原因
          </text>
        </view>
        <view class="input-box reason">
          {{ dataForm.offlineReason }}
        </view>
      </uni-forms-item>
      <uni-forms-item
        name="representative"
        class="setting-info-item"
      >
        <view class="title">
          <text class="txt">
            下线时间
          </text>
        </view>
        <view class="input-box">
          {{ dataForm.createTime }}
        </view>
      </uni-forms-item>
      <uni-forms-item
        v-if="dataForm.status === 1 || dataForm.status === 4"
        name="capital"
        class="setting-info-item capital"
      >
        <view class="title">
          <text class="star">
            *
          </text>
          <text class="txt">
            申请理由
          </text>
        </view>
        <view class="application-input-box application-input-res">
          <textarea
            v-model="reapplyReason"
            style="height: 100%"
          />
        </view>
      </uni-forms-item>
      <uni-forms-item
        v-if="hasHis"
        name="capital"
        class="setting-info-item capital"
      >
        <view class="title">
          <text class="txt">
            申请历史
          </text>
        </view>
        <view
          v-for="item in dataForm.offlineHandleEventItemList"
          :key="item.eventItemId"
          class="input-box-his"
        >
          <view v-if="item.createTime">
            申请时间：{{ item.createTime }}
          </view>
          <view
            v-if="item.reapplyReason"
            class="reason"
          >
            申请理由：{{ item.reapplyReason }}
          </view>
          <view v-if="item.auditTime">
            审核时间：{{ item.auditTime }}
          </view>
          <view
            v-if="item.refuseReason"
            class="reason"
          >
            拒绝原因：{{ item.refuseReason }}
          </view>
        </view>
      </uni-forms-item>

      <view class="footer">
        <button
          v-if="dataForm.status === 1 || dataForm.status === 4"
          type="default"
          hover-class="none"
          @tap="dataFormSubmit(dataForm.status !== 1 && dataForm.status !== 4)"
        >
          提交申请
        </button>
      </view>
    </uni-forms>
  </view>
</template>

<script setup>
// <!-- 1平台进行下线 2 重新申请，等待审核 3.审核通过 4 审核未通过 -->
const statusList = reactive({
  1: '违规下架',
  2: '等待审核',
  3: '已通过',
  4: '审核不通过'
})

let prodId = ''
onLoad((options) => {
  prodId = options.prodId
  getOrderInfoById()
})

/**
 * 获取详情
 */
const dataForm = ref({})
const hasHis = ref(true)
const getOrderInfoById = () => {
  const params = {
    url: `/prod/prod/getOfflineHandleEventByProdId/${prodId}`,
    method: 'GET'
  }
  http.request(params).then(({ data }) => {
    hasHis.value = data.offlineHandleEventItemList.filter(x => x.createTime).length > 0
    dataForm.value = data
    uni.setNavigationBarTitle({
      title: (data.status === 1 || data.status === 4 ? '申请上架' : '审核历史')
    })
  })
}
/**
 * 提交
 */
const reapplyReason = ref('')
const dataFormSubmit = (status) => {
  const reg = /^\s+$/g
  if (status) {
    return
  }
  if (!reapplyReason.value || !reapplyReason.value.trim()) {
    uni.showToast({
      title: '申请理由不能为空',
      mask: true,
      icon: 'none',
      duration: 1200
    })
    return
  }
  if (reg.test(reapplyReason.value)) {
    uni.showToast({
      title: '内容不能全为空格',
      mask: true,
      icon: 'none',
      duration: 1200
    })
    return
  }
  uni.showModal({
    title: '提示',
    content: '确定提交申请吗？',
    cancelText: '取消',
    confirmText: '确定',
    success (res) {
      if (res.confirm) {
        const params = {
          url: '/prod/prod/auditApply',
          method: 'post',
          data: {
            eventId: dataForm.value.eventId,
            handleId: prodId,
            reapplyReason: reapplyReason.value
          }
        }
        http.request(params).then(() => {
          uni.showToast({
            title: '操作成功',
            mask: true,
            icon: 'none',
            duration: 1200
          })
          setTimeout(() => {
            uni.navigateBack(1)
          }, 1200)
        })
      }
    }
  })
}

</script>

<style lang="scss" scoped>
@use './prod-application.scss';
</style>
